package game;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import entities.NPC;

public class Map {
	
	int[][] map;
	Image tree;
	Image frontWall, sideWall;
	Image woodFloor;
	Image metalDoor, metalDoorOpen;
	
	public Map(int width, int height, File file) throws IOException, SlickException {
		map = new int[width][height];
		
		BufferedReader in = new BufferedReader(new FileReader(file));
		for (int y = 0; y < height; y++) {
			String line = in.readLine();
			for (int x = 0; x < width; x++) {
				if (line.charAt(x) == '1') {
					map[x][y] = 1;
				}
				if (line.charAt(x) == '2') {
					map[x][y] = 2;
				}
				if (line.charAt(x) == '3') {
					map[x][y] = 3;
				}
				if (line.charAt(x) == '4') {
					map[x][y] = 4;
				}
				if (line.charAt(x) == '5') {
					map[x][y] = 5;
				}
			}
		}
		
		frontWall = new Image("assets/img/walls/frontWall.png");
		sideWall = new Image("assets/img/walls/sideWall.png");
		woodFloor = new Image("assets/img/floors/woodFloor.png");
		metalDoor = new Image("assets/img/doors/metalDoor.png");
		metalDoorOpen = new Image("assets/img/doors/metalDoorOpen.png");
	}
	
	public boolean collide(int x, int y) {
		return map[x][y] == 1 || map[x][y] == 2 || map[x][y] == 3; 
	}
	
	public boolean isDoor(int x, int y) {
		return map[x][y] == 5;
	}
	
	public boolean isNPC(int x, int y) {
		for (NPC npc : GameState.entities) {
			if (npc.x == x && npc.y == y) {
				return true;
			}
		}
		return false;
	}
	
	public void draw(Graphics g) {
		for (int x = 0; x < map.length; x++) {
			for (int y = 0; y < map[x].length; y++) {
				switch (map[x][y]) {
				case 1:
					g.drawImage(tree, x * 32, y * 32);
					break;
				case 2:
					g.drawImage(frontWall, x * 32, y * 32);
					break;
				case 3:
					g.drawImage(sideWall, x * 32, y * 32);
					break;
				case 4:
					g.drawImage(woodFloor, x * 32, y * 32);
					break;
				case 5:
					g.drawImage(metalDoor, x * 32, y * 32);
				}
			}
		}
	}
}